<template>
	<vue-office-excel
		:key="index"
		:src="url"
		:options="options"
		style="height: 100%"
		@rendered="rendered"
		@error="error" />
</template>

<script>
	import '@vue-office/excel/lib/index.css';
	import VueOfficeExcel from '@vue-office/excel';
	import { downFile } from '@/api/system/oss';
	export default {
		props: {
			src: {
				required: true,
			},
			options: {
				required: true,
			},
		},
		components: { VueOfficeExcel },
		data() {
			return {
				url: '',
				index: 0,
			};
		},
		created() {
			if (this.src) {
				if (this.src.includes('blob:')) {
					this.url = this.src;
					return;
				}
				downFile(this.src)
					.then(res => {
						if (res) {
							this.url = URL.createObjectURL(res);
						} else {
							throw new Error();
						}
					})
					.catch(err => {
						this.error();
					});
			}
		},
		methods: {
			resize() {
				this.index++;
				if (this.index > 10) {
					this.index = 0;
				}
			},
			rendered() {
				this.$emit('rendered');
			},
			error() {
				this.$emit('error');
			},
		},
	};
</script>

<style lang="scss" scoped></style>
